﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Medianamik.Core;
using Telerik.Web.UI;
using Medianamik.UI.Controls;
using Medianamik.Core.Sugar;

namespace Medianamik.UI.ConfigManagement
{
    public partial class SiteList : Medianamik.UI.BasePage
    {
        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnCultureChanged(CultureChangedEventArgs e)
        {
            grdSites.Rebind();
        }

        protected void grdSites_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                using (GridDataItem gridItem = (GridDataItem)e.Item)
                {
                    if (gridItem.DataItem != null)
                    {
                        WebSite node = (WebSite)gridItem.DataItem;

                        ConfirmedDeleteButton button = (ConfirmedDeleteButton)e.Item.FindControl("btnDeleteSite");

                        if (button != null)
                        {
                            button.Enabled = !node.HasChildren;
                        }
                    }
                }
            }
        }

        protected void grdSites_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            string strNodeId = e.CommandArgument.ToString();

            Guid nodeId;

            if (StringHelper.IsGuid(strNodeId, out nodeId))
            {
                Node node = NodeManager.GetNode(nodeId);

                if (node != null)
                {
                    NodeManager.DeleteNode(node.ID);
                }
            }

            grdSites.Rebind();
        }

        protected void grdSites_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

        }

        protected void grdSites_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            grdSites.DataSource = ConfigManager.GetAllWebSites(CurrentEditionCulture);
        }

        #endregion
    }
}
